package com.baibei.ebec.sdk;

import android.content.Context;

import com.baibei.ebec.sdk.interceptor.ApiTokenInterceptor;
import com.baibei.ebec.sdk.interceptor.RequestInterceptor;
import com.baibei.ebec.sdk.interceptor.ResponseInterceptor;
import com.baibei.model.UserInfo;
import com.baibei.sdk.RetrofitApi;
import com.squareup.okhttp3.OkHttpExtBuilder;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

/**
 * 接口提供程序
 * Created by ChenRui on 2017/7/27 0027 13:44.
 */
class ApiProviderImpl extends RetrofitApi implements ApiProvider {

    private OkHttpClient httpClient;

    ApiProviderImpl(Context applicationContext) {
        super(applicationContext);
    }

    @Override
    protected Retrofit onCreate(Context context) {


        httpClient = new OkHttpExtBuilder()
                .https()
//                .cache(context)
                .debug("BB-API")
                .build()
                .addInterceptor(new ApiTokenInterceptor())
                .addInterceptor(new RequestInterceptor(context, mConfig))
                .addInterceptor(new ResponseInterceptor(mConfig))
                .connectTimeout(45, TimeUnit.SECONDS)
                .build();

        return new Retrofit.Builder()
                .client(httpClient)
                .baseUrl(ApiConstant.API_BASE_URL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(ApiConverterFactory.create())
                .build();

    }

    private <T> T create(Class<T> cls) {
        return mRetrofit.create(cls);
    }


    @Override
    public ITokenApi getTokenApi() {
        return create(ITokenApi.class);
    }

    @Override
    public IUserApi getUserApi() {
        return create(IUserApi.class);
    }

    @Override
    public IProductApi getProductApi() {
        return create(IProductApi.class);
    }

    @Override
    public IPushApi getPushApi() {
        return create(IPushApi.class);
    }

    @Override
    public IWelfareApi getWelfareApi() {
        return create(IWelfareApi.class);
    }

    @Override
    public IQuotationApi getQuotationApi() {
        return create(IQuotationApi.class);
    }

    @Override
    public ITradeApi getTradeApi() {
        return create(ITradeApi.class);
    }
    @Override
    public IMessageApi getMessageApi() {
        return create(IMessageApi.class);
    }
    @Override
    public void clear() {
        clearAuth();
    }

//    @Override
//    public void clearConfig() {
//        ApiConstant.APP_U_KEY_VALUE = null;
//        mConfig.clearAll();
//    }

//    @Override
//    public void cancelAll() {
//        try {
//            if (httpClient != null && httpClient.dispatcher() != null) {
//                int count = httpClient.dispatcher().runningCallsCount();
//                if (count > 1)
//                    httpClient.dispatcher().cancelAll();
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    @Override
    public OkHttpClient client() {
        return httpClient;
    }

    @Override
    public IOrderApi getOrderApi() {
        return create(IOrderApi.class);
    }

    @Override
    protected Class<?> createTokenClass() {
        return TokenInfo.class;
    }

    @Override
    protected Class<?> createUserClass() {
        return UserInfo.class;
    }
}
